package DesignMode.Strategy;

import jdk.internal.org.objectweb.asm.tree.IincInsnNode;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author VX5
 * @Title: MJC
 * @ProjectName DataStructure
 * @Description: TODO
 * @date ${DAT}15:57
 */
public class StrategyFactory {
    private Map<Integer,Strategy> map;

    public StrategyFactory(){
        List<Strategy> strategies = new ArrayList<>();

        strategies.add(new OrdinaryStrategy());
        strategies.add(new SilverStategy());

        //java8 流特性
        map = strategies.stream().collect(Collectors.toMap(Strategy::getType,strategy -> strategy));

         /* 等同上面
        map = new HashMap<>();
        for (Strategy strategy : strategies) {
            map.put(strategy.getType(), strategy);
        }*/
    }

    //单例模式
    public static class Holder{
        public static StrategyFactory instance = new StrategyFactory();
    }

    public static StrategyFactory getInstance(){
        return Holder.instance;
    }

    public Strategy get(Integer type){
        return map.get(type);
    }
    private static double getResult(long money, int type) {

        if (money < 1000) {
            return money;
        }

        Strategy strategy = StrategyFactory.getInstance().get(type);

        if (strategy == null){
            throw new IllegalArgumentException("please input right type");
        }

        return strategy.compute(money);
    }
}
